package com.zy.web.service.impl;

import com.zy.common.utils.sql.StringUtils;
import com.zy.common.core.page.TableDataInfo;
import com.zy.common.core.domain.PageQuery;
import com.zy.web.domain.bo.ZyResultBo;
import com.zy.web.domain.vo.ZyResultVo;
import com.zy.web.repository.ZyResultRepository;
import com.zy.web.service.IZyResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.zy.web.domain.ZyResult;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 结果Service业务层处理
 *
 * @author zy
 * @date 2024-02-25
 */
@RequiredArgsConstructor
@Service
public class ZyResultServiceImpl implements IZyResultService {

    private final ZyResultRepository repository;

    /**
     * 查询结果
     */
    @Override
    public ZyResultVo queryById(String id){
        return repository.findById(id).map(v -> com.zy.common.utils.BeanCopyUtils.copy(v, ZyResultVo.class)).orElse(null);
    }

    /**
     * 查询结果列表
     */
    @Override
    public TableDataInfo<ZyResultVo> queryPageList(ZyResultBo bo, PageQuery pageQuery) {
        Page<ZyResult> result = repository.findAll(getSpecification(bo), pageQuery.pageable());
        return TableDataInfo.build(result, ZyResultVo.class);
    }

    /**
     * 查询结果列表
     */
    @Override
    public List<ZyResultVo> queryList(ZyResultBo bo) {
        return repository.findAll(getSpecification(bo)).stream()
            .map(v -> com.zy.common.utils.BeanCopyUtils.copy(v, ZyResultVo.class))
            .collect(Collectors.toList());
    }

    private Specification<ZyResult> getSpecification(ZyResultBo bo) {
        return (root, query, cb) -> {
            Map<String, Object> params = bo.getParams();
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(bo.getId())) {
                    predicates.add(cb.equal(root.get("id"), bo.getId()));
            }
            if (bo.getCreateBy() != null) {
                    predicates.add(cb.equal(root.get("createBy"), bo.getCreateBy()));
            }
            if (bo.getCreateTime() != null) {
                    predicates.add(cb.equal(root.get("createTime"), bo.getCreateTime()));
            }
            if (bo.getUpdateBy() != null) {
                    predicates.add(cb.equal(root.get("updateBy"), bo.getUpdateBy()));
            }
            if (bo.getUpdateTime() != null) {
                    predicates.add(cb.equal(root.get("updateTime"), bo.getUpdateTime()));
            }
            if (StringUtils.isNotBlank(bo.getFirstPlaceName())) {
                    predicates.add(cb.like(root.get("firstPlaceName"), "%" + bo.getFirstPlaceName() + "%"));
            }
            if (bo.getFirstPlaceTime() != null) {
                    predicates.add(cb.equal(root.get("firstPlaceTime"), bo.getFirstPlaceTime()));
            }
            if (StringUtils.isNotBlank(bo.getFourthPlaceName())) {
                    predicates.add(cb.like(root.get("fourthPlaceName"), "%" + bo.getFourthPlaceName() + "%"));
            }
            if (bo.getFourthPlaceTime() != null) {
                    predicates.add(cb.equal(root.get("fourthPlaceTime"), bo.getFourthPlaceTime()));
            }
            if (StringUtils.isNotBlank(bo.getProjectName())) {
                    predicates.add(cb.like(root.get("projectName"), "%" + bo.getProjectName() + "%"));
            }
            if (StringUtils.isNotBlank(bo.getProjectType())) {
                    predicates.add(cb.equal(root.get("projectType"), bo.getProjectType()));
            }
            if (StringUtils.isNotBlank(bo.getSecondPlaceName())) {
                    predicates.add(cb.like(root.get("secondPlaceName"), "%" + bo.getSecondPlaceName() + "%"));
            }
            if (bo.getSecondPlaceTime() != null) {
                    predicates.add(cb.equal(root.get("secondPlaceTime"), bo.getSecondPlaceTime()));
            }
            if (StringUtils.isNotBlank(bo.getThirdPlaceName())) {
                    predicates.add(cb.like(root.get("thirdPlaceName"), "%" + bo.getThirdPlaceName() + "%"));
            }
            if (bo.getThirdPlaceTime() != null) {
                    predicates.add(cb.equal(root.get("thirdPlaceTime"), bo.getThirdPlaceTime()));
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
    }

    /**
     * 新增结果
     */
    @Override
    public Boolean insertByBo(ZyResultBo bo) {
        ZyResult add = com.zy.common.utils.BeanCopyUtils.copy(bo, ZyResult.class);
        validEntityBeforeSave(add);
        repository.saveAndFlush(add);
        bo.setId(add.getId());
        return true;
    }

    /**
     * 修改结果
     */
    @Override
    public Boolean updateByBo(ZyResultBo bo) {
        ZyResult update = com.zy.common.utils.BeanCopyUtils.copy(bo, ZyResult.class);
        validEntityBeforeSave(update);
        return repository.save(update) != null;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZyResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        repository.deleteAllById(ids);
        return true;
    }

}
